﻿using System;
using System.Net;
using System.Windows;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Linq;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using Corina.Commands;
using System.Windows.Resources;
using System.Windows.Markup;
using System.IO;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class TopurlViewmodel : ViewmodelBase
    {
        public TopurlViewmodel()
        {
            WeburlClient.GetAllTopurlCompleted += GetAllTopurlCompleted;
            WeburlClient.RemoveTopurlCompleted += RemoveTopurlCompleted;
            WeburlClient.UpdateTopurlCompleted += UpdateTopurlCompleted;
            WeburlClient.InsertTopurlCompleted += InsertTopurlCompleted;
            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        #region Methods
        private void InsertTopurlCompleted(object sender, DataService.InsertTopurlCompletedEventArgs e)
        {
            UpdateEntity = new WeburlTopInfo();
            Operation = EntityOperation.None;
            LoadData();
        }

        private void UpdateTopurlCompleted(object sender, DataService.UpdateTopurlCompletedEventArgs e)
        {

            UpdateEntity = new WeburlTopInfo();
            Operation = EntityOperation.None;
            LoadData();
        }

        private void RemoveTopurlCompleted(object sender, DataService.RemoveTopurlCompletedEventArgs e)
        {
            LoadData();
        }

        protected override void LoadData(params object[] paras)
        {
            IsBusy = true;
            WeburlClient.GetAllTopurlAsync(Account, Password, "", SelectedPlatform);
        }

        protected override void OperationChanged()
        {
            RaisePropertyChanged(() => ShowUpdate);
        }

        protected override void CurrentIndexChanged()
        {
            RaisePropertyChanged(() => ShowCollection);
        }

        private void GetAllTopurlCompleted(object sender, DataService.GetAllTopurlCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.Result == ResultValue.Succeed)
                {
                    ItemsCollection = new ObservableCollection<WeburlTopInfo>(e.Result.Entities.OfType<WeburlTopInfo>());
                    Keywords = string.Empty;
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
            IsBusy = false;
        } 
        #endregion

        #region Properties
        private string _keywords = string.Empty;
        public string Keywords
        {
            get { return _keywords; }
            set
            {
                _keywords = value;
                RaisePropertyChanged(() => Keywords);
                Index = 1;
                RaisePropertyChanged(() => ShowCollection);
            }
        }

        private ObservableCollection<WeburlTopInfo> _itemsCollection;
        public ObservableCollection<WeburlTopInfo> ItemsCollection
        {
            get { return _itemsCollection; }
            set
            {
                _itemsCollection = value;
                RaisePropertyChanged(() => ItemsCollection);
                RaisePropertyChanged(() => ShowCollection);
            }
        }

        public IEnumerable<WeburlTopInfo> ShowCollection
        {
            get
            {
                var items = ItemsCollection == null ? null : ItemsCollection.Where(t=>t.Title.Contains(Keywords)||t.IdentifyCode.Contains(Keywords));
                ItemsCount = items == null ? 0 : items.Count();
                return items == null ? null : items.Skip((Index - 1) * 20).Take(20);
            }
        }

        private PlatformTypeValue _selectedPlatform = PlatformTypeValue.Manager;
        public PlatformTypeValue SelectedPlatform
        {
            get { return _selectedPlatform; }
            set
            {
                _selectedPlatform = value;
                RaisePropertyChanged(() => SelectedPlatform);
                LoadData();
            }
        }

        private WeburlTopInfo _updateEntity = new WeburlTopInfo();
        public WeburlTopInfo UpdateEntity
        {
            get { return _updateEntity; }
            set
            {
                _updateEntity = value;
                RaisePropertyChanged(() => UpdateEntity);
            }
        }
        #endregion

        #region Commands
        public ICommand RemoveCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    if (MessageBox.Show("确定要删除该链接吗？", "删除确认", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        int id = Convert.ToInt32(t);
                        WeburlClient.RemoveTopurlAsync(Account, Password, id);
                    }
                });
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);
                    UpdateEntity = ItemsCollection.FirstOrDefault(i => i.Id == id);
                    Operation = EntityOperation.Update;
                });
            }
        }

        public ICommand InsertCommand
        {
            get
            {
                return new GenericCommand()
                {
                    
                    ExecuteCallback = delegate
                    {
                        UpdateEntity = new WeburlTopInfo();
                        Operation = EntityOperation.Insert;
                    }
                };
            }
        }

        public ICommand CancelCommand
        {
            get {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        Operation = EntityOperation.None;
                        UpdateEntity = new WeburlTopInfo();
                    }
                };
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        if (Operation == EntityOperation.Insert)
                        {
                            WeburlClient.InsertTopurlAsync(Account, Password, UpdateEntity.Title, UpdateEntity.Url, UpdateEntity.Icon, UpdateEntity.IdentifyCode, UpdateEntity.PlatformType, UpdateEntity.Index, UpdateEntity.ShowToAll);
                        }
                        else
                        {
                            WeburlClient.UpdateTopurlAsync(Account, Password, UpdateEntity.Title, UpdateEntity.Url, UpdateEntity.Icon, UpdateEntity.IdentifyCode, UpdateEntity.Index, UpdateEntity.ShowToAll, UpdateEntity.Id);
                        }
                    }
                };
            }
        }
        #endregion
    }
}
